package com.thinksns.sociax.t4.android.presenter;


import com.thinksns.sociax.android.R;
import com.thinksns.sociax.modle.CashConfig;
import com.thinksns.sociax.t4.android.Thinksns;
import com.thinksns.sociax.t4.android.view.GoldWithDrawView;
import com.thinksns.sociax.t4.unit.UiUtils;
import com.thinksns.sociax.thinksnsbase.network.ApiHttpClient;

/**
 * Created by jungle on 16/11/15.
 */
public class GoldWithDrawPresenter {
    private GoldWithDrawView mRootView;
    private Thinksns app = Thinksns.getApplication();

    public GoldWithDrawPresenter(GoldWithDrawView rootView) {
        this.mRootView = rootView;
        getConfig();
    }

    /**
     * 提现
     *
     * @param gold
     * @param amount
     */
    public void exchange(final String gold, String amount) {
        mRootView.showLoading();
        app.getApplicationApi().createOrder(gold, amount, new ApiHttpClient.HttpRespListener<String>() {
            @Override
            public void onSuccess(String result) {
                notifyUser(UiUtils.getString(R.string.str_gold_with_submit_success));
                mRootView.updatedGold(gold);
            }

            @Override
            public void onError(Throwable result) {
                notifyUser(UiUtils.getString(R.string.net_work_error));
            }

            @Override
            public void onFail(int code, String msg) {
                notifyUser(msg);
            }
        });
    }


    /**
     * 获取配置信息
     */
    private void getConfig() {
        mRootView.showLoading();
        app.getApplicationApi().getZBConfig(null, new ApiHttpClient.HttpRespListener<CashConfig>() {
            @Override
            public void onSuccess(CashConfig result) {
                mRootView.hideLoading();
                mRootView.initRegular(result);
            }

            @Override
            public void onError(Throwable result) {
                notifyUser(UiUtils.getString(R.string.net_work_error));
            }

            @Override
            public void onFail(int code, String msg) {
                notifyUser(msg);

            }
        });
    }

    /**
     * 通知用户更新界面
     *
     * @param msg
     */
    private void notifyUser(String msg) {
        mRootView.hideLoading();
        mRootView.showMessage(msg);
    }

}
